package cn.y.operation.application.web.controller;

import cn.y.operation.application.core.entity.SysUser;
import cn.y.operation.application.web.service.SysUserService;
import cn.y.operation.framework.constants.Constants;
import cn.y.operation.framework.enums.ResponseMsgEnum;
import cn.y.operation.framework.mvc.BaseController;
import cn.y.operation.framework.mvc.ResponseMsg;
import cn.y.operation.framework.utils.dev.StringUtils;
import cn.y.operation.framework.utils.system.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName: OpenController
 * @author: 叶子豪
 * @create: 2021-03-01 11:17
 */
@Controller
@RequestMapping("/")
@Api(tags = "开放相关接口")
public class OpenController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(OpenController.class);

    @Resource
    public SysUserService sysUserService;

    @PostMapping("/captcha")
    @ResponseBody
    public boolean captcha(HttpServletRequest request, String captcha) {
        Object correctCode = request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        System.out.println("系统生成的验证码是:" + correctCode);
        return true;//captcha.equals(correctCode);
    }

    @GetMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        if (UserUtils.isAuthenticated()) {
            return "/index";
        } else {
            return "login/login";
        }
    }

    @PostMapping("/login")
    @ResponseBody
    public ResponseMsg ajaxLogin(String username, String password, Boolean rememberMe) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return success("欢迎：" + UserUtils.getUserName());
        } catch (AuthenticationException e) {
            return error(ResponseMsgEnum.USERNAME_OR_PASSWORD_ERROR.getInfo());
        }
    }

    @GetMapping("/logout")
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "login/login";
    }

    /**
     * 用户注册
     */
    @ApiOperation("用户注册")
    @PostMapping("/registered")
    @ResponseBody
    public ResponseMsg registered(@Validated SysUser user) {
        String result = sysUserService.registered(user);
        return returnResult(result);
    }

    @ApiOperation("激活码验证")
    @PostMapping("/verifyRegistration")
    @ResponseBody
    public ResponseMsg verifyRegistration(@RequestParam String loginName, @RequestParam String verificationCode) {
        String result = sysUserService.verifyRegistration(loginName, verificationCode);
        if (StringUtils.isEmpty(result)) {
            return sysUserService.activateUser(loginName) ? success() : error();
        } else {
            return error(result);
        }
    }
}